﻿// See https://aka.ms/new-console-template for more information
namespace TowerOfHanoi
{
    internal class Program
    { /// <summary>
      /// 用递归算法实现汉诺塔
      /// </summary>
      /// <param name="args"></param>
        static void Main( )
        {
            string A = "A"; string B = "B"; string C = "C";
            //初始化
            Console.WriteLine("请输入汉诺塔层数，层数大于2");
            int n = Convert.ToInt32(Console.ReadLine());
            TowerOfHanoi(n, A, B, C);
        }
        static void TowerOfHanoi(int n, string A, string B, string C)
        {
           //仅有一个盘
            if (n == 1)
            { 
            Console.WriteLine($"移动{n}号盘从{A}移到{C}");
            } 
            //不止一个盘
            else
            {
                //将n-1个盘从塔A经过塔C移动到塔B上
                TowerOfHanoi(n - 1, A, C, B);
                //A中最大的盘从塔A移动到塔C
                Console.WriteLine($"移动{n}号盘从{A}移到{C}"); 
                //将塔B上的n-1个盘移动到塔C上
                TowerOfHanoi(n - 1, B, A, C);
            }
        }
    }
}